Javascript replace space with regex


\s : means "one space"

\s+ : means "one or more spaces"

let stringWithSpace = '  A B  C   D EF ';

console.log(str.replace(/\s/g, '#'));                                
// ##A#B##C###D#EF# => means one space replace by one #;

console.log(str.replace(/\s+/g, '#'));                             
// #A#B#C#D#EF#  => means one or more spaces, all replace by #;
#javascript #replace #RegEx






你可能感興趣的文章

筆記:What the heck is the event loop anyway | Philip Roberts | JSConf EU

筆記:What the heck is the event loop anyway | Philip Roberts | JSConf EU

電腦科學概論筆記2 迴圈

電腦科學概論筆記2 迴圈

【Day00】系列介紹

【Day00】系列介紹






留言討論